package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.ItemType;
import cn.stylefeng.guns.sys.modular.system.mapper.ItemTypeMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.ItemTypeParam;
import cn.stylefeng.guns.sys.modular.system.model.result.ItemTypeResult;
import cn.stylefeng.guns.sys.modular.system.service.ItemTypeService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 品类表 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2020-09-13
 */
@Service
public class ItemTypeServiceImpl extends ServiceImpl<ItemTypeMapper, ItemType> implements ItemTypeService {
    private Logger logger = LoggerFactory.getLogger(ItemTypeServiceImpl.class);
    private RestTemplate restTemplate = new RestTemplate();


    @Override
    public void add(ItemTypeParam param){
        ItemType entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ItemTypeParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ItemTypeParam param){
        ItemType oldEntity = getOldEntity(param);
        ItemType newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ItemTypeResult findBySpec(ItemTypeParam param){
        return null;
    }

    @Override
    public List<ItemTypeResult> findListBySpec(ItemTypeParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ItemTypeParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void initItemTypeData() {
        String url = "http://btyjt.c3.fxscm.net/cldpoint/getItemType.do?ent=ENT7617&username=admin&password=admin123";
        String content = restTemplate.getForObject(url, String.class);
        JSONObject jsonObjectResult = JSON.parseObject(content);
        //数据转换
        List<ItemType> itemTypeList = JSON.parseArray(jsonObjectResult.getString("data"), ItemType.class);

        if(!CollectionUtils.isEmpty(itemTypeList)){
            //重新保存之前先把之前所有的删除
            this.baseMapper.truncateData();
            this.saveBatch(itemTypeList);
        }

        logger.info("初始化品类数据：{}条数据",itemTypeList.size());
    }

    private Serializable getKey(ItemTypeParam param){
        return null;
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ItemType getOldEntity(ItemTypeParam param) {
        return this.getById(getKey(param));
    }

    private ItemType getEntity(ItemTypeParam param) {
        ItemType entity = new ItemType();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
